package com.atguigu.gulimall.seckill.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory)
            throws UnknownHostException {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(factory);
        // 自定义Jackson2JsonRedisSerializer
        StringRedisSerializer serializer = new StringRedisSerializer();
        template.setValueSerializer(serializer);
        //设置hash的key的序列化为JSON
        template.setHashKeySerializer(serializer);
        //设置hash的value的序列化为JSON
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();

        return template;
    }

}